import re, revcalc

class NotImplementedException(Exception):
    pass

class Repository:
    def __init__(self, path, URL=None):
        self.path = path
        self.URL = URL
    #add this method to the repository class and also to cvs
    def buildRepUsers(self, db, repLogFileName):
        raise NotImplementedException
            
    #create the log file for the repository at the path
    #and write it to logFilename
    def createLog(self, path, logFilename):
        raise NotImplementedException()
        
    #get a list of all of the developer names from the logFilename
    def getDevNames(self, logFilename):
        raise NotImplementedException()
        
    #parse the given log file and fill the database with commit information 
    def parseLog(self, db, repLogFileName):
        raise NotImplementedException()
        
    #update the file on disk to the given revision from the repository
    def updateFileToRev(self, filename, revision):
        raise NotImplementedException()
        
    #takes a date object and updates the cvspath to it's state at that date
    def updateToDate(date, cvsPath):
        "takes in numerical date and year and updates the cvs working copy"
        
    #known extensions for known source file types
    sourceExts = ['.h','.c','.cpp','.pl','.py','.java'] 
    def isInteresting(self, filename):
        mo = re.search('\.[^.^/^\n]*$',filename)
        if (mo != None):
            #print mo.group(0)
            if(self.sourceExts.count(mo.group(0)) > 0) :
                return True
        #This is boring. You're Boring.
        return False
    def prepUpdate(self):
        "does whatever is nessisary to make the update of any file possible...cvs needs all dir's present"
        raise NotImplementedException()

class Function:
    def __init__(self, funcID, name, start, end):
        self.funcID = funcID
        self.name = name
        self.start = start
        self.end = end
class Change:
    def __init__(self, before, after, changeType):
        self.before = before #pair of line numbers (start/end of changes in first rev)
        self.after = after #pair of line numbers (start/end of changes in last rev)
        self.changeType = changeType #char: a, d, or c

class FileRevision:
    "Holds data about the change from one revision to the next:"
    def __init__(self, rev1, rev2):
        self.initialRev = rev1 #revisions string
        self.finalRev = rev2    
        
class RepFile:
    "Holds data about a single file"
    def __init__(self, filename):
        self.filename = filename
        self.fileRevList = [] #list of file revisions
        
    def addRev(self, revision):
        if revision.find('.') > -1:
            #we have a . so we're a cvs revisiona
            if revision == '1.1' or revision == '1.1.1.1':
                self.fileRevList.append(FileRevision(revcalc.previous(revision), revision))
            else:
                self.fileRevList.append(FileRevision(None, revision))
        else:
            #svn case
            if int(revision) > 1:
                self.fileRevList.append(FileRevision(str(int(revision)-1), revision))
            else:
                self.fileRevList.append(FileRevision(None, revision))
